<template>
    <div class="account">
        <el-row :gutter="24">
            <el-col :span="8">
                <div class="header-view">
                    <div class="group-title">店铺抽成级数设置</div>
                    <el-button type="primary" size="mini" @click="addStoreShareConfig">新增层级</el-button>
                </div>
                <el-table :data="storeShareConfigs" border style="margin-bottom: 48px" :height="300">
                    <el-table-column label="层级" align="center" prop="hierarchy" width="80" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="层级说明" align="left" prop="name" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="抽成比例" align="center" prop="dividendRatio" width="90"
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{ scope.row.dividendRatio * 100 }} %
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="100" fixed="right">
                        <template slot-scope="scope">
                            <el-button-group>
                              <div>
                                <el-button type="text" icon="el-icon-edit"
                                    @click="editStoreShareConfig(scope.row)"></el-button>
                                <el-button type="text" v-if="maxStoreHierarchy === scope.row.hierarchy"
                                    icon="el-icon-delete" @click="deleteShareConfig(scope.row)"></el-button>
                              </div>
                            </el-button-group>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
<!--            <el-col :span="6">
              <div class="header-view">
                <div class="group-title">平台默认返点率</div>
              </div>
              <el-table :data="returnAttr_1" border :height="300">
                <el-table-column label="对象" align="center" prop="name" >
                </el-table-column>
                <el-table-column label="返点率" align="center" width="120" prop="value" >
                </el-table-column>
                <el-table-column label="操作" align="center" width="100" fixed="right">
                  <template slot-scope="scope">
                    <el-button-group>
                      <el-button type="text" icon="el-icon-edit"
                                 @click="editUserShareConfig(scope.row)"></el-button>
                    </el-button-group>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>-->
            <el-col :span="8">
              <div class="header-view">
                <div class="group-title table-title">设置店铺</div>
              </div>
              <el-table :data="returnAttr" :height="300" >
                <el-table-column label="对象" align="center" prop="name" >
                </el-table-column>
                <el-table-column label="返点率" align="center" width="120" prop="value" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                  <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="setReturn(scope.row)"
                               v-hasPermi="['car:level:edit']">修改</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
        </el-row>
          <el-row class="mini-btn">
            <el-col :span="16">

              <div class="header-view">
                  <div class="group-title table-title">店铺等级</div>
                  <el-button type="primary" size="mini" @click="addStoreLevel">新增层级</el-button>
              </div>
              <el-table v-loading="loadingStore" :data="storeLevelList" style="margin-bottom: 48px" border
                  :height="700">
<!--                  <el-table-column label="小店等级" align="center" prop="id" min-width="120" :show-overflow-tooltip="true" />-->
                  <el-table-column label="等级名" align="center" prop="name" min-width="120" :show-overflow-tooltip="true" />
                  <el-table-column label="等级图标" align="center" prop="icon" min-width="120" :show-overflow-tooltip="true">
                      <template slot-scope="scope">
                          <oss-image-preview :model="'text'" :src="scope.row.icon" />
                      </template>
                  </el-table-column>
                <el-table-column label="先款后货" align="center" prop="cashBeforeDelivery" min-width="120"
                                 :show-overflow-tooltip="true">
                  <template slot-scope="scope">
                    <el-switch v-model="scope.row.cashBeforeDelivery" active-color="#13ce66"
                               inactive-color="#ff4949" :active-value="1" :inactive-value="2"
                               @change="storeLevelStatusChange(scope.row)" />
                  </template>
                </el-table-column>
                <el-table-column label="先货后款" align="center" prop="deliveryBeforeCash" min-width="120"
                                 :show-overflow-tooltip="true">
                  <template slot-scope="scope">
                    <el-switch v-model="scope.row.deliveryBeforeCash" active-color="#13ce66"
                               inactive-color="#ff4949" :active-value="1" :inactive-value="2"
                               @change="storeLevelStatusChange(scope.row)" />
                  </template></el-table-column>
                <el-table-column label="任务发起" align="center" prop="sellTaskFlag" min-width="120"
                                 :show-overflow-tooltip="true">
                  <template slot-scope="scope">
                   <el-switch v-model="scope.row.sellTaskFlag" active-color="#13ce66"
                               inactive-color="#ff4949" :active-value="1" :inactive-value="2"
                               @change="storeLevelStatusChange(scope.row)" />
                  </template>
                </el-table-column>
                <el-table-column label="平台活动" align="center" prop="activityFlag" min-width="120"
                                 :show-overflow-tooltip="true">
                  <template slot-scope="scope">
                    <el-switch v-model="scope.row.activityFlag" active-color="#13ce66"
                               inactive-color="#ff4949" :active-value="1" :inactive-value="2"
                               @change="storeLevelStatusChange(scope.row)" />
                  </template>
                </el-table-column>

                <el-table-column label="升级奖励" align="center" prop="upgradeReward" min-width="120"
                                 :show-overflow-tooltip="true" />
                <el-table-column label="平台抽成" align="center" prop="platformRatio" min-width="120"
                                 :show-overflow-tooltip="true" />
                <el-table-column label="最低盈利点数" align="center" prop="minIncomeRatio" min-width="120"
                                 :show-overflow-tooltip="true" />
                <el-table-column label="最高盈利点数" align="center" prop="maxIncomeRatio" min-width="120"
                                 :show-overflow-tooltip="true" />
                <el-table-column label="最小采购数" align="center" prop="purchaseMinNum" min-width="120"
                                 :show-overflow-tooltip="true" />
                <el-table-column label="最小采购金额" align="center" prop="purchaseMinAmount" min-width="120"
                                 :show-overflow-tooltip="true" />
                <el-table-column label="最大采购金额" align="center" prop="purchaseMaxAmount" min-width="120"
                                 :show-overflow-tooltip="true" />
                <el-table-column label="月采购额度" align="center" prop="purchaseQuota" min-width="120"
                                 :show-overflow-tooltip="true" />
<!--                  <el-table-column label="先款折扣" align="center" prop="" min-width="120"
                      :show-overflow-tooltip="true" />
                  <el-table-column label="收益天数" align="center" prop="" min-width="120"
                      :show-overflow-tooltip="true" />
                  <el-table-column label="收益范围" align="center" prop="" min-width="120"
                      :show-overflow-tooltip="true" />-->
                <el-table-column label="收益天数" align="center" prop="incomeDays" min-width="120"
                                 :show-overflow-tooltip="true" />
                  <el-table-column label="邀请人数" align="center" prop="upgradeConditionNum" min-width="120"
                      :show-overflow-tooltip="true" />
                <el-table-column label="升级保证金" align="center" prop="earnestMoney" min-width="120"
                                 :show-overflow-tooltip="true" :formatter="formatterEarnestMoney"/>
<!--                  <el-table-column label="最低收益比例" align="center" prop="minIncomeRatio" min-width="160"
                      :show-overflow-tooltip="true" />
                  <el-table-column label="收益比例" align="center" prop="incomeRatio" min-width="120"
                      :show-overflow-tooltip="true" />
                  <el-table-column label="等级" align="center" prop="level" min-width="80" :show-overflow-tooltip="true" />-->
                  <!-- <el-table-column label="下级Id" align="center" prop="nextLevel" /> -->
                  <el-table-column label="操作" align="center" fixed="right" width="200">
                      <template slot-scope="scope">
                          <el-button size="mini" type="text" icon="el-icon-edit"
                              @click="updateStore(scope.row)">修改</el-button>
                          <el-button size="mini" type="text" icon="el-icon-delete" v-if="scope.row.id == maxStoreLevel"
                              @click="deleteStore(scope.row)">删除</el-button>
                      </template>
                  </el-table-column>
              </el-table>
            </el-col>

          </el-row>

<!--          店铺充值区间-->
      <el-row class="mini-btn">
        <el-col :span="16">
          <div class="header-view">
            <div class="group-title table-title">店铺充值区间</div>
            <el-button type="primary" size="mini" @click="updateStoreRecharge({},1)">新增区间</el-button>
          </div>
          <el-table :data="gradientList" style="margin-bottom: 48px" border
                    :height="700">
<!--            <el-table-column label="分割点" align="center" prop="splitPoint" min-width="120" :show-overflow-tooltip="true" />-->

            <el-table-column label="充值区间" align="center" prop="minPoint" min-width="120"
                             :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span v-if="scope.row.maxPoint">${{scope.row.minPoint}}(包含) ~ ${{scope.row.maxPoint}}(不包含)</span>
               <span v-else>≥${{scope.row.minPoint}}(包含)</span>
              </template>
            </el-table-column>
            <el-table-column label="返现金额" align="center" prop="amount" min-width="120"
                             :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span>${{scope.row.amount.toFixed(2)}}</span>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" min-width="120"
                             :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.status" active-color="#13ce66"
                           inactive-color="#ff4949" :active-value="1" :inactive-value="0"
                           @change="storeRechargeStatusChange(scope.row)" />
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="200">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit"
                           @click="updateStoreRecharge(scope.row,2)">修改</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>

      </el-row>
      <!-- 编辑层级 -->
        <el-dialog :title="storeShareEditTitle" :visible.sync="storeShareOpen" width="600px" :close-on-click-modal="false">
            <el-form :model="storeShareForm" ref="storeShareForm" label-position="right" :inline="true" label-width="160"
                :rules="storeShareFormRules">
                <el-form-item label="层级名字" prop="name">
                    <el-input v-model="storeShareForm.name" style="width: 400px"></el-input>
                </el-form-item>
                <el-form-item prop="dividendRatio">
                    <span slot="label">
                        <el-tooltip content="最大值为1, 输入0.01表示1%" placement="top">
                            <i class="el-icon-question"></i>
                        </el-tooltip>
                        抽成比例
                    </span>

                    <el-input-number v-model="storeShareForm.dividendRatio" :precision="2" :step="0.01" :min="0" :max="1"
                        style="width: 400px"></el-input-number>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="saveStoreConfig">
                        确定
                    </el-button>
                    <!-- <el-button @click="storeShareOpen = false"> -->
                    <el-button @click="closeDialog('storeShareForm', 'storeShareOpen')">
                        取消
                    </el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <!-- 添加或修改店铺等级对话框 -->
        <el-dialog :title="titleStore" :visible.sync="openStore" width="1000px" append-to-body>
            <el-form ref="storeForm" :model="storeForm" :rules="storeRules" label-width="110px" label-position="left">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="等级名" prop="name">
                            <el-input class="modal-input" v-model="storeForm.name" placeholder="请输入等级名" />
                        </el-form-item>
                    </el-col>
                  <el-col :span="12">
                    <el-form-item label="收益天数" prop="incomeDays">
                      <el-input type="number" class="modal-input" v-model="storeForm.incomeDays" placeholder="请输入收益天数" />
                    </el-form-item>
                  </el-col>
                    <!-- <el-col :span="12">
                        <el-form-item label="等级" prop="level">
                            <el-input class="modal-input" v-model="storeForm.level" placeholder="请输入等级" />
                        </el-form-item>
                    </el-col> -->
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="最低盈利点数" prop="minIncomeRatio">
                      <el-input type="number" :step="0.01" :min="0" :max="0.99" class="modal-input" v-model="storeForm.minIncomeRatio" placeholder="请输入最低盈利点数" />
                    </el-form-item>
                  </el-col>
                    <el-col :span="12">
                        <el-form-item label="最高盈利点数" prop="maxIncomeRatio">
                            <el-input type="number" :step="0.01" :min="0" :max="0.99" class="modal-input" v-model="storeForm.maxIncomeRatio" placeholder="请输入最高盈利点数" />
                        </el-form-item>
                    </el-col>
                </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="最小采购金额" prop="purchaseMinAmount">
                    <el-input type="number" class="modal-input" v-model="storeForm.purchaseMinAmount" placeholder="请输入最小采购金额" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="最大采购金额" prop="purchaseMaxAmount">
                    <el-input type="number" class="modal-input" v-model="storeForm.purchaseMaxAmount" placeholder="请输入最大采购金额" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="最小采购数" prop="purchaseMinNum">
                    <el-input type="number" class="modal-input" v-model="storeForm.purchaseMinNum" placeholder="请输入最小采购数" />
                  </el-form-item>
                </el-col>

                <el-col :span="12">
                  <el-form-item label="月采购额度" prop="purchaseQuota">
                    <el-input type="number" class="modal-input" v-model="storeForm.purchaseQuota" placeholder="请输入月采购额度" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-form-item label="平台抽成" prop="platformRatio">
                  <el-input type="number" :step="0.01" :min="0" :max="0.99" class="modal-input" v-model="storeForm.platformRatio" placeholder="请输入平台抽成" />
                </el-form-item>
                <el-form-item label="升级奖励" prop="upgradeReward">
                  <el-input type="number" :step="0" :min="0" class="modal-input" v-model="storeForm.upgradeReward" placeholder="请输入升级奖励" />
                </el-form-item>

              </el-row>
                <el-row>
                    <el-form-item label="等级图标" prop="icon">
                        <ImageUpload :limit="1" v-model="storeForm.icon"></ImageUpload>
                    </el-form-item>
                </el-row>
              <el-divider content-position="center">升级条件</el-divider>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="有效下级人数" prop="upgradeConditionNum">
                    <el-input type="number" min="0" class="modal-input" v-model="storeForm.upgradeConditionNum"
                              placeholder="请输入有效下级人数" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="保证金" prop="earnestMoney">
                    <el-input type="number" step="1" class="modal-input" v-model="storeForm.earnestMoney" placeholder="请输入保证金" />
                  </el-form-item>
                </el-col>
<!--                <el-col :span="12">
                  <el-form-item label="下级" prop="nextLevel">
                    <el-select v-model="storeForm.nextLevel" class="modal-input">
                      <el-option v-for="(item, index) in storeLevelList" :key="index" :label="item.name"
                                 :value="item.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>-->
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitStoreForm">确 定</el-button>
                <el-button @click="cancelStoreForm">取 消</el-button>
            </div>
        </el-dialog>
<!--        返点率-->
      <el-dialog title="设置返点率" :visible.sync="openReturn" width="500px" append-to-body>
        <el-form ref="returnForm" :model="returnForm" label-position="left">
          <el-form-item :label="currentReturn.name" prop="value" :rules="[{ required: true, message: '返点率不能为空', trigger: 'blur' }]">
            <el-input-number type="number" class="modal-input" v-if="currentReturn.type === 4" :precision="2" :min="0" v-model="returnForm.value" placeholder="请输入返点率"/>
            <el-input-number type="number" class="modal-input" v-else :precision="2" :step="0.01" :min="0" :max="1" v-model="returnForm.value" placeholder="请输入返点率"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitReturnForm">确 定</el-button>
          <el-button @click="cancelReturnForm">取 消</el-button>
        </div>
      </el-dialog>
<!--    修改店铺充值区间-->
      <el-dialog :title="titleRecharge" :visible.sync="showRecharge" width="500px" append-to-body @close="$refs['RechargeForm'].clearValidate()">
        <el-form ref="RechargeForm" :model="gradientObj" label-position="left">
          <el-form-item label="最小充值金额" prop="splitPoint" :rules="[{ required: true, message: '最小充值金额不能为空', trigger: 'blur' }]">
            <el-input-number type="number" class="modal-input" :min="0" v-model="gradientObj.splitPoint" placeholder="请输入最小充值金额"/>
          </el-form-item>
          <el-form-item label="返现金额" prop="amount" :rules="[{ required: true, message: '返现金额不能为空', trigger: 'blur' }]">
            <el-input-number type="number" class="modal-input" :min="0" v-model="gradientObj.amount" placeholder="请输入返现金额	"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitRechargeForm">确 定</el-button>
          <el-button @click="$refs['RechargeForm'].clearValidate();showRecharge=false">取 消</el-button>
        </div>
      </el-dialog>

    </div>
</template>

<script>
import {
  configList,
  addShareConfig,
  updateConfig,
  delConfig,
  storeLevelList,
  updateStoreLevel,
  addStoreLevel,
  storeLevelDetails,
  platform, anchor, 
  advancePayment, 
  rechargeGradient, 
  rechargeGradientAdd, 
  rechargeGradientStatus,
  effectiveShop
} from '@/api/store/gradeRebate'
import OssImagePreview from "@/components/ImagePreview/oss-image-preview.vue";

export default {
    name: "Index",
    components: { OssImagePreview },
    data() {
        return {
            maxTabelHeight: '',
            //图片上传地址
            // uploadUrl: getUploadUrl(),
            //图片域名
            // baseUrl: getBaseUrl(),
            headerObj: {
                //   Authorization: getToken(),
            },
            // 遮罩层
            loading: true,
            // 用户表格数据
            dataList: null,
            // 总条数
            // total: 0,
            // 排序
            orderType: true,
            storeShareConfigs: [],
            storeShareForm: {},
            storeShareOpen: false,
            storeShareEditTitle: "",
            storeShareFormRules: {
                name: [
                    {
                        required: true,
                        message: "请输入层级名称",
                        trigger: "change,blur",
                    },
                ],
                dividendRatio: [
                    {
                        required: true,
                        message: "请输入抽成比例",
                        trigger: "change,blur",
                    },
                ],
            },
            //商铺等级列表
            storeLevelList: [],
            openStore: false,

            titleStore: '',
            // 表单参数
            storeForm: {},
            // 表单校验
            storeRules: {
                name: { required: true, message: '请输入店铺等级名称', trigger: 'blur' },
                icon: { required: true, message: '请上传店铺等级图标', trigger: 'blur' },
                upgradeConditionNum: { required: true, message: '请输入有效下级人数', trigger: 'blur' },
                ratio: [{ required: true, message: '请输入佣金比例', trigger: 'blur' },{ pattern: /^1|0|[0]{1}[.]{1}[0]*[1-9]+[0-9]*$/, message:"佣金比例范围0-1", trigger: ['blur', 'change'] }],
                minIncomeRatio: [{ required: true, message: '请输入最低收益比例', trigger: 'blur' },{ pattern: /^1|0|[0]{1}[.]{1}[0]*[1-9]+[0-9]*$/, message:"最低收益比例范围0-1", trigger: ['blur', 'change'] }],
                nextLevel: { required: true, message: '请选择下级ID', trigger: ['blur', 'change']},
              earnestMoney: [{ required: true, message: '请输入保证金', trigger: 'blur' }],
              platformRatio: [{ required: true, message: '请输入平台抽成', trigger: 'blur' },{ pattern: /^1|0|[0]{1}[.]{1}[0]*[1-9]+[0-9]*$/, message:"平台抽成范围0-1", trigger: ['blur', 'change'] }],
              incomeRatio: [{ required: true, message: '请输入收益点数', trigger: 'blur' },{ pattern: /^1|0|[0]{1}[.]{1}[0]*[1-9]+[0-9]*$/, message:"收益比例范围0-1", trigger: ['blur', 'change'] }],
              upgradeReward: [{ required: true, message: '请输入升级奖励', trigger: 'blur' }],
              maxIncomeRatio:[{ required: true, message: '请输入最高盈利点数', trigger: 'blur' }],
              purchaseMinNum:[{ required: true, message: '请输入最小采购数', trigger: 'blur' }],
              purchaseMinAmount:[{ required: true, message: '请输入最小采购金额', trigger: 'blur' }],
              purchaseMaxAmount:[{ required: true, message: '请输入最大采购金额', trigger: 'blur' }],
              purchaseQuota:[{ required: true, message: '请输入月采购额度', trigger: 'blur' }],
              incomeDays:[{ required: true, message: '请输入收益天数', trigger: 'blur' }],
            },
          maxStoreLevel: 0,
          maxUserLevel: 0,
          returnAttr:[{name:'平台默认返点率',value:0,type:1},
            {name:'主播默认返点率',value:0,type:2},
            {name:'进货先款折扣',value:0,type:3},
            {name:'有效店铺余额',value:0,type:4}],
          openReturn:false,
          currentReturn:{},
          returnForm:{},
          gradientList:[],
          showRecharge:false,
          gradientObj:{
              status:0,
          },
          titleRecharge:'',
          rechargeType:null,
        };
    },
    created() {
        //   // type 0代表店铺抽成 1代表个人分佣
        this.getShareConfig();
        this.getStoreList();
        this.getShopRecharge();
    },
    computed: {
        maxStoreHierarchy() {
            let hierarchy = 0;
            for (let item of this.storeShareConfigs) {
                    if (item.hierarchy >= hierarchy) {
                        hierarchy = item.hierarchy;
                    }
               /*  if (item.type === 0) {
                } */
            }
            return hierarchy;
        },
        maxUserHierarchy() {
            let hierarchy = 0;
            for (let item of this.userShareConfigs) {
                if (item.type === 1) {
                    if (item.hierarchy >= hierarchy) {
                        hierarchy = item.hierarchy;
                    }
                }
            }
            return hierarchy;
        },
    },
    methods: {
      // 获取所有分割区间
      getShopRecharge(){
        rechargeGradient().then(res => {
          this.gradientList = res.data
          console.log('res.data',this.gradientList)
        })
      },
      submitRechargeForm(){
        this.$refs["RechargeForm"].validate(valid => {
          if (valid) {
            rechargeGradientAdd(this.gradientObj).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.showRecharge = false;
              this.getShopRecharge();
            });
          }
        });
      },
        getShareConfig() {
            configList().then((res) => {
              console.log(res);
                    this.storeShareConfigs = res.data.hierarchies || [];
                    this.returnAttr = [{name:'平台默认返点率',value:res.data.platform,type:1},
                    {name:'主播默认返点率',value:res.data.anchor,type:2},
                    {name:'进货先款折扣',value:res.data.advancePayment,type:3},
                    {name:'有效店铺余额',value:res.data.effectiveShop,type:4},];
               /*  if (type == "0") {
                } else {
                    this.userShareConfigs = res.data || [];
                } */
            });
        },
        addStoreShareConfig() {
            this.storeShareOpen = true;
            this.storeShareEditTitle = "新增抽成层级";
            this.storeShareForm = {};
        },
        editStoreShareConfig(row) {
            this.storeShareEditTitle = "修改抽成百分比";
            this.storeShareForm = Object.assign({}, row);
            this.storeShareOpen = true;
        },
        saveStoreConfig() {
            this.$refs["storeShareForm"].validate((valid) => {
                if (valid) {
                    if (this.storeShareForm.id) {
                        //如果是总分红不校验
                        const total =
                            this.totalDividendRatio(this.storeShareForm.hierarchy, "0") +
                            this.storeShareForm.dividendRatio;
                        if (total > 1) {
                            this.$message({
                                type: "error",
                                message: '抽成百分比总和不能大于100%',
                            });
                            return;
                        }

                        updateConfig(this.storeShareForm).then((res) => {
                            this.$message({ type: "success", message: '修改成功！' });
                            this.getShareConfig();
                            this.resetStoreConfigForm();
                        });
                    } else {
                        // 新增
                        const total =
                            this.totalDividendRatio(null, "0") +
                            this.storeShareForm.dividendRatio;
                        if (total > 1) {
                            this.$message({
                                type: "error",
                                message: '抽成百分比总和不能大于100%',
                            });
                            return;
                        }
                        this.storeShareForm.hierarchy = this.maxStoreHierarchy + 1;
                        this.storeShareForm.type = 0;
                        addShareConfig(this.storeShareForm).then((res) => {
                            this.$message({ type: "success", message: '添加成功！' });
                            this.getShareConfig();
                            this.resetStoreConfigForm();
                        });
                    }
                }
            });
        },
        resetStoreConfigForm() {
            this.storeShareOpen = false;
            this.storeShareForm = {};
            this.$refs["storeShareForm"].resetFields();
        },

        deleteShareConfig(row) {
            this.$modal
                .confirm("确认删除" + row.hierarchy + "的数据项？")
                .then(function () {
                    return delConfig(row.id);
                })
                .then(() => {
                    this.getShareConfig();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => { });
        },

        totalDividendRatio(hierarchy, type) {
                let total = 0;
                for (let item of this.storeShareConfigs) {
                    if (hierarchy && hierarchy === item.hierarchy) {
                        continue;
                    }
                    total += item.dividendRatio;
                }
                return total;
            if (type == "0") {
            } else {
                let total = 0;
                for (let item of this.userShareConfigs) {
                    if (hierarchy && hierarchy === item.hierarchy) {
                        continue;
                    }
                    total += item.dividendRatio;
                }
                return total;
            }
        },
        clearForm() {
            this.$refs.formPop.resetFields()
            this.$refs.formPop.clearValidate();
        },

        closeDialog(form, open) {
            this.$refs[form].resetFields();
            this.$refs[form].clearValidate();
            this[open] = false;
        },
        //   店铺部分
        /** 查询店铺等级列表 */
        getStoreList() {
            this.loadingStore = true;
            storeLevelList().then(response => {
                this.storeLevelList = response.data;
                this.maxStoreLevel = Math.max(...this.storeLevelList.map(i => i.id));
                this.loadingStore = false;
            });
        },
        // 取消按钮
        cancelStoreForm() {
            this.openStore = false;
            this.resetStoreForm();
        },
        // 表单重置
        resetStoreForm() {
            this.storeForm = {
                id: null,
                name: null,
                icon: null,
                upgradeConditionNum: null,
                ratio: null,
                minIncomeRatio: null,
                incomeRatio: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                level: null,
                nextLevel: null,
                earnestMoney: null,
                cashBeforeDelivery: null,
                deliveryBeforeCash: null
            };
            // this.resetForm("storeForm");
        },
        /** 新增按钮操作 */
        addStoreLevel() {
            this.resetStoreForm();
            this.openStore = true;
            this.titleStore = "添加店铺等级";
        },
        /** 修改按钮操作 */
        updateStore(row) {
            this.resetStoreForm();
            const id = row.id || this.ids
            this.openStore = true;
            this.storeForm = {...row};
            this.titleStore = "修改店铺等级";
        },
      updateStoreRecharge(row,type){
          type === 1 ? this.titleRecharge = '新增充值区间' : this.titleRecharge = '修改充值区间';
          this.gradientObj = {...row}
          this.rechargeType = type;
          this.showRecharge = true;
      },

        /** 删除按钮操作 */
        deleteStore(row) {
            // storeLevelDetails(id).then(response => {
            //     this.storeForm = response.data;
            //     this.openStore = true;
            //     this.titleStore = "修改店铺等级";
            // });
            this.$modal
                .confirm("确认删除" + row.name + "的数据项？")
                .then(function () {
                    return storeLevelDetails(row.id);
                })
                .then(() => {
                    this.getStoreList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => { });
        },
        /** 提交按钮 */
        submitStoreForm() {
            this.$refs["storeForm"].validate(valid => {
                if (valid) {
                    if (this.storeForm.id != null) {
                        updateStoreLevel(this.storeForm).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.openStore = false;
                            this.getStoreList();
                        });
                    } else {
                        addStoreLevel(this.storeForm).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.openStore = false;
                            this.getStoreList();
                        });
                    }
                }
            });
        },
        // 状态修改
        storeLevelStatusChange(row) {
            updateStoreLevel(row).then(res => {
                if (res.success) {
                    this.getStoreList();
                    this.$message.success(res.msg);
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
      //状态区间修改
      storeRechargeStatusChange(row){
        rechargeGradientStatus(row).then(res => {
          if (res.success) {
            this.getShopRecharge();
            this.$message.success(res.msg);
          } else {
            this.$message.error(res.msg)
          }
        })

      },
        formatterEarnestMoney:function(row){
            return this.$yun.priceFormatter(row.earnestMoney);
        },
      setReturn(row){
          this.currentReturn = row;
          this.returnForm = row;
          this.openReturn = true;
      },
      submitReturnForm(){
          this.$refs["returnForm"].validate(valid => {
            if (valid) {
              let res = null;
               if(this.currentReturn.type === 1){//平台返点率
                platform({ platform: this.returnForm.value }).then(res => {
                  if(res.success){
                    this.$message({ type: "success", message: '设置成功！' });
                    this.openReturn = false;
                    this.getShareConfig();
                  }
                })
              }else if(this.currentReturn.type === 2){
                anchor({anchor:this.returnForm.value}).then(res => {
                  if(res.success){
                    this.$message({ type: "success", message: '设置成功！' });
                    this.openReturn = false;
                    this.getShareConfig();
                  }
                })
              }else if(this.currentReturn.type === 3){
                 advancePayment({advancePayment:this.returnForm.value}).then(res => {
                   if(res.success){
                     this.$message({ type: "success", message: '设置成功！' });
                     this.openReturn = false;
                     this.getShareConfig();
                   }
                 })
               }else if(this.currentReturn.type === 4){
                 effectiveShop({effectiveShop:this.returnForm.value}).then(res => {
                   if(res.success){
                     this.$message({ type: "success", message: '设置成功！' });
                     this.openReturn = false;
                     this.getShareConfig();
                   }
                 })
               }
            }
          })
      },
      cancelReturnForm(){
        this.openReturn = false;
        this.returnForm = {};
        this.$refs.returnForm.resetFields()
        this.$refs.returnForm.clearValidate();
      },
    },
};
</script>

<style lang="scss" scoped>
.account {
    padding: 10px;
}

.header-view {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    height: 44px;
  .table-title{
    height: 40px;
    padding-top: 0 !important;
  }
}

.group-title {
    font-size: 18px;
    font-weight: 700;
    color: #333333;
}

.el-table {
    width: auto !important;
}

.table-title {
    height: 44px;
    line-height: 44px;
}
</style>
